// pages/insuranceAppo/insuranceAppo.js
import config from "../../config.js";
var util = require('../../utils/util.js');
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadImg: "https://app.longchg.com/gudongcar/images/upload.jpg",
    //城市列表
    carList: [],
    carIndex: 0,
    typeList: [],
    typeIndex: 0,
    backAddress: "",
    getAddress: "",
    orderType: 0,
    latitude: 0,
    longitude: 0,
    defaultKeeper: {
      name: '',
      header: '',
      houseKeeperId: null
    },
    date: util.getDate(),
    mindate: util.getDate(),
    time: util.getMinute(),
    mintime: util.getMinute(),
    // 是否上牌
    hasNo: true,
    tel: "",
    name: "",
    orderId: -1,
    order: null,
    isFirst: true
  },
  dateChange: function(e) {
    var time = util.getMinute();
    var date = e.detail.value;
    var currentDate = util.getDate();
    var mintime = this.data.mintime;



    if (date != currentDate) {
      mintime = "";
    } else {
      mintime = util.getMinute();
      this.setData({
        time: time
      })
    }


    this.setData({
      date: e.detail.value,
      mintime: mintime,
    })

  },
  nameChange: function(e) {
    this.setData({
      name: e.detail.value,
    })
  },


  timeChange: function(e) {
    this.setData({
      time: e.detail.value,
    })
  },


  getTypeList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.stepServiceTypeList,
      data: {
        start: 0,
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            typeList: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },
  submit: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var user = wx.getStorageSync("user");
    console.log(user);
    var houseKeeperId = that.data.defaultKeeper.houseKeeperId;
    console.log(houseKeeperId);
    var orderType = 2;
    console.log(orderType);
    var serviceType = that.data.typeList[that.data.typeIndex].typeId;
    // console.log(serviceType);
    var carId = that.data.carList[that.data.carIndex].cid;
    console.log(carId);
    // var comId = 0;
    // console.log(comId);
    var orderFrom = 1;
    console.log(orderFrom);
    var addressReturn = that.data.getAddress;
    if (that.data.getAddress == "") {
      wx.showToast({
        title: "请选择救援地点",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }

    if (that.data.name == "") {
      wx.showToast({
        title: "请输入联系人",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    if (that.data.tel == "") {
      wx.showToast({
        title: "请输入联系电话",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    console.log(addressReturn);
    var tel = user.tel;

    console.log(tel);
    var time = that.data.date + " " + that.data.time;
    console.log(time);


    wx.request({
      url: openIdUrl.accidentAssistSubbmit,
      data: {
        houseKeeperId: houseKeeperId,
        orderType: orderType,
        serviceType: serviceType,
        orderId: that.data.orderId,
        carId: carId,
        // stepCarId: carId,
        // comId: comId,
        pickTime: time,
        orderFrom: orderFrom,
        address: addressReturn,
        addressPick: that.data.getAddress,
        addressReturn: that.data.backAddress,
        tel: that.data.tel,
        linker: that.data.name,
        rescureTime: time,
        locLatitude: that.data.latitude,
        locLongitude: that.data.longitude,
        locLatitudePick: that.data.latitude,
        locLongitudePick: that.data.longitude,
        locLatitudeReturn: that.data.latitude,
        locLongitudeReturn: that.data.longitude,
        items: 0
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          var orderId = wx.getStorageSync("orderId");
          wx.navigateTo({
            url: "../orderSuccess/success?id=" + orderId,
            complete: function() {
              wx.setStorageSync("orderConfirmType", null);
              wx.setStorageSync("orderId", null);
            }
          })
          // wx.showModal({
          //   title: "恭喜您",
          //   content: "预约成功，车管家将会在10分钟内通过电话与您沟通详情 ，请保持电话畅通！谢谢！",
          //   showCancel: false,
          //   confirmColor: "#137AF0",
          //   success: function () {

          //   }
          // })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })

  },
  getCarList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.myCarsList,
      data: {
        // id: 1,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            carList: res.data.data
          })

          //订单服务
          if (that.data.order != null) {
            for (var i = 0; i < that.data.carList.length; i++) {
              var car = that.data.carList[i];
              if (that.data.order.order.cars != null) {
                if (that.data.order.order.cars.cid == car.cid) {
                  that.setData({
                    carIndex: i
                  })
                }
              }

            }
          }

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },

  carChange: function(e) {
    this.setData({
      carIndex: e.detail.value,
    })
  },

  typeChange: function(e) {
    this.setData({
      typeIndex: e.detail.value,
    })
  },

  servChange: function(e) {
    this.setData({
      servIndex: e.detail.value,
    })
  },

  telChange: function(e) {
    this.setData({
      tel: e.detail.value,
    })
  },
  getChange: function(e) {
    this.setData({
      getAddress: e.detail.value,
    })
  },

  selectNo: function() {
    let flag = this.data.hasNo;
    if (flag) {
      this.setData({
        hasNo: false
      })
    } else {
      this.setData({
        hasNo: true
      })
    }
  },



  toAppoPage: function() {
    wx.navigateTo({
      url: "../insurPlan/insurPlan",
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //判断是否是再次下单或者更改订单类型
    var orderId = options.orderId;
    this.setData({
      date: util.getDate(),
      time: util.getMinute(),
      tel: wx.getStorageSync("user").tel,
      name: wx.getStorageSync("user").name,
    })
    if (orderId == null) {
      if (wx.getStorageSync('user') != null) {
        that.setData({
          'defaultKeeper.name': wx.getStorageSync("user").housekeeper.name,
          'defaultKeeper.header': wx.getStorageSync("user").housekeeper.image,
          'defaultKeeper.houseKeeperId': wx.getStorageSync("user").housekeeper.keeperId
        })
      }
    } else {
      that.setData({
        orderId: orderId
      })

      //获取订单详情
      that.orderDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    this.getTypeList();
    var that = this;
    setTimeout(function() {
      that.getCarList();
    }, 300);


    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })

    if (!that.data.isFirst) {
      var houseKeeper = wx.getStorageSync("houseKeeper");
      if (houseKeeper) {
        this.setData({
          "defaultKeeper.name": houseKeeper.name,
          "defaultKeeper.houseKeeperId": houseKeeper.keeperId,
          "defaultKeeper.header": houseKeeper.image,
        })
        console.log(this.data.mainInfo);
      }
    }

    that.setData({
      isFirst: false
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    wx.setStorageSync("houseKeeper", null);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("houseKeeper", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  orderAction() {
    wx.showModal({
      title: "恭喜您",
      content: "预约成功，车管家将会在10分钟内通过电话与您沟通详情 ，请保持电话畅通！谢谢！",
      showCancel: false,
      confirmColor: "#137AF0",
      success: function() {
        wx.switchTab({
          url: "../order/order",
        })
      }
    })
  },
  chooseMap: function() {
    var that = this;
    util.checkLocationIsOff(this)
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          getAddress: res.address
        })
      },
    })

  },
  chooseMap2: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          backAddress: res.address
        })
      }
    })
  },
  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: openIdUrl.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data
          })
          that.setOrderData();
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  setOrderData() {
    var that = this;
    //订单信息
    var order = that.data.order.order;
    var service = that.data.order.service;
    that.setData({
      latitude: service.locLatitude,
      longitude: service.locLongitude,
      getAddress: service.address,
      name: service.linker,
      tel: service.tel,
    })

    var houseKeeper = order.housekeeper;
    if (houseKeeper) {
      this.setData({
        "defaultKeeper.name": houseKeeper.name,
        "defaultKeeper.houseKeeperId": houseKeeper.keeperId,
        "defaultKeeper.header": houseKeeper.image,
      })
    }

    that.getCarList();
  },
  changeKeeper: function() {
    var instance = this;
    wx.navigateTo({
      url: '../managerList/managerList',
    })
  },
})